//
//  WDWorkOrderSearchMarketView.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/8/19.
//

#import "WDWorkOrderSearchMarketView.h"
#import "WDHistorySearchWordsModel.h"

@interface WDWorkOrderSearchMarketView ()

//@property (nonatomic, strong) NSArray *dataList;
@property (nonatomic, strong) NSMutableArray *buttonList;
@property (nonatomic, copy) NSString *title;

@property (nonatomic, strong) UILabel *titleLabel;

/// 清空
@property (nonatomic, strong) UIButton *btnClear;

@end

@implementation WDWorkOrderSearchMarketView

- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title
{
    self.title = title;
    
    return [self initWithFrame:frame];
}

- (void)initWithVar
{
    _buttonList = [NSMutableArray new];
}

- (void)initWithSubView
{
    self.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
    [self initWithTitleLabel];
    
    [self initWithBtnClear];
}

- (void)initWithTitleLabel
{
    CGRect frame = CGRectMake(15, 0, (self.width - 30) * 0.5, 22);
    _titleLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
        label.textColor = [UIColor colorWithHexString:@"#000000"];
    }];
    _titleLabel.text = self.title;
}

- (void)initWithBtnClear
{
    UIImage *image = [UIImage imageNamed:@"btn_browse_delete"];
    CGRect frame = CGRectMake(self.width - image.size.width - 15, 0, image.size.width, image.size.height);
    _btnClear = [UIButton createWithButton:self frame:frame font:FONTSIZE(16) action:@selector(actionClear:) propertiesBlock:^(UIButton * _Nonnull button) {
        [button setImage:image forState:UIControlStateNormal];
    }];
}

- (void)updateWithViewData:(id)data completion:(void (^)(float))completion
{
    [self.dataList removeAllObjects];
    [self.dataList addObjectsFromArray:data];
    
    for (UIButton *btn in self.buttonList)
    {
        [btn removeFromSuperview];
        btn.hidden = YES;
    }
    [self.buttonList removeAllObjects];
    
    float leftSpace = 15;
    float midSpace = 12;
    float allWidth = self.width - 15;
    float singleWidth = 0;
    UIFont *font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
    UIColor *color = [UIColor colorWithHexString:@"#000000"];
    UIColor *bgColor = [UIColor colorWithHexString:@"#F5F5F5"];
    UIButton *btnPre = nil;
    CGRect frame = CGRectMake(leftSpace, self.titleLabel.bottom + 11, 0, kWDWorkOrderSearchMarketViewHeight);
    
    for (int i=0; i<self.dataList.count; i++)
    {
        NSString *keyWord = @"";
        id model = self.dataList[i];
        if ([model isKindOfClass:[TYZCommonCellModel class]])
        {
            TYZCommonCellModel *ent = model;
            keyWord = ent.title;
        }
        else if ([model isKindOfClass:[WDHistorySearchWordsModel class]])
        {
            WDHistorySearchWordsModel *ent = model;
            keyWord = ent.keyWord;
        }
        else if ([model isKindOfClass:[NSString class]])
        {
            keyWord = model;
        }
        
        singleWidth = [keyWord widthForFont:font] + 32;
        frame.size.width = singleWidth;
        if (btnPre)
        {
            if (singleWidth > kScreenW - 30)
            { // 换行
                singleWidth = kScreenW - 30;
                frame.size.width = singleWidth;
                frame.origin.x = leftSpace;
                frame.origin.y = btnPre.bottom + midSpace;
            }
            else if (btnPre.right + midSpace + singleWidth <= allWidth)
            {// 不换行
                frame.origin.x = btnPre.right + midSpace;
            }
            else
            {
                frame.origin.x = btnPre.right + midSpace;
                frame.origin.y = btnPre.bottom + midSpace;
                frame.origin.x = leftSpace;
            }
        }
        else
        {
            if (singleWidth > kScreenW - 30)
            { // 换行
                singleWidth = kScreenW - 30;
                frame.size.width = singleWidth;
            }
        }
        
        UIButton *btnMark = [UIButton createWithButton:self frame:frame font:font action:@selector(actionClicked:) propertiesBlock:^(UIButton * _Nonnull button) {
            [button setTitle:keyWord forState:UIControlStateNormal];
            [button setTitleColor:color forState:UIControlStateNormal];
            button.layer.cornerRadius = 3;//frame.size.height * 0.5;
            button.layer.masksToBounds = YES;
            button.backgroundColor = bgColor;
        }];
        btnMark.tag = 100 + i;
        btnPre = btnMark;
        [self.buttonList addObject:btnMark];
    }
    
    if (btnPre)
    {
        self.height = btnPre.bottom + 3;
    }
    if (completion)
    {
        completion(self.height);
    }
}

- (void)actionClicked:(id)sender
{
    UIButton *btn = (UIButton *)sender;
    NSInteger index = btn.tag - 100;
    if (self.touchMarketBlock) {
        self.touchMarketBlock(self.title, btn.titleLabel.text, index);
    }
}

/// 清空
/// @param sender sender description
- (void)actionClear:(id)sender
{
    if (self.touchClearMarketBlock)
    {
        self.touchClearMarketBlock();
    }
}

- (void)setHiddenClearBtn:(BOOL)hiddenClearBtn
{
    _hiddenClearBtn = hiddenClearBtn;
    _btnClear.hidden = hiddenClearBtn;
}

@end
